define(function(require, exports, module) {
	
	var procinstTpl=require('resources/template/workflow/procinst_tpl.html');
	require('resources/js/jquery.dataTables.min.js');
	require('resources/js/jquery.dataTables.bootstrap.js');
	require('resources/js/jquery.dataTables.buttons.js');
	require('resources/js/jquery.json-2.4.min.js');
	require('resources/js/jquery.gritter.min.js');
	var DTConstant=require('resources/template/common/DTConstant.js');
	var ACEConstant=require('resources/template/common/ACEConstant.js');

	var ProcinstView=require('resources/template/workflow/procinstView.js');
	
	var Procinst=function($container,config,data,parent){
		this.getUrl=Common.getUrl.bind(this);
		this.config=$.extend({},Procinst.DEFAULT,config);
		this.data=data;
		this.parent=parent;
		this.$container=$container;
		this.$div=$(procinstTpl).prependTo(this.$container);
		this.init();
	};
	
	Procinst.prototype={
		urls:{
			listR:'/procinst/listR.jhtml',
			listH:'/procinst/listH.jhtml',
			deleteProcinstR:'/procinst/deleteR.jhtml',
			deleteProcinstH:'/procinst/deleteH.jhtml',
			updateStateProcinst:'/procinst/updateState.jhtml',
			viewProcinst:'/procinst/view.jhtml?procinstId='
		},	
		init:function(){
			//初始化-列表
			this.initListR();
			this.initListH();
		},
		initListH:function(){
			var that=this;
			this.$listH=$('.procinstHList',this.$div);
			this.tableH =this.$listH.DataTable($.extend(true,{},DTConstant.DEFAULT,{
				ajax:{
				    url:this.getUrl('listH'),
				    contentType: "application/json",
				    type: "POST",
				    data: function ( d ) {
				      return $.toJSON(d);
				    }
				},
				rowId:'id',
				columns:[{data:'id',title:''},
				         {data: 'name' ,title:'名称'},
				         {data: 'processDefinitionName' ,title:'流程定义'},
				         {data: 'startTime' ,title:DTConstant.TITLE.DATETIME+'开始时间'},
				         {data: 'endTime' ,title:DTConstant.TITLE.DATETIME+'结束时间'},
				         {data: 'duration',title:'历时(秒)'},
				         {data: 'deleteReason',title:'结束原因'}
				],
				columnDefs: [DTConstant.COLUMN.RADIO],
				buttons:{
					buttons: [{
						text: '查看',
						className: 'btn btn-sm btn-success icon-eye-open',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							that.parent.toSubPage(ProcinstView,{
									procinstId:record.id,
									hasParentPage:true
								},'历史流程查看');
						}
					},{
						text: '删除',
						className: 'btn btn-sm btn-success icon-trash',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							var title='删除历史流程实例：'+(record.name||'');
							$.ajax(that.getUrl('deleteProcinstH'),{
								type:'POST',
								dataType:'json',
								data:{procinstId:record.id}
							}).done(function(data){//成功
								if(data.result)
								{
									that.refreshListH();
									ACEConstant.operateSuccess(title);
								}	
								else
									ACEConstant.operateFailed(title,'操作失败：'+data.note);							
							}).fail(function(data){//失败
								ACEConstant.operateFailed(title);
							});
						}
					},{
						text: '刷新',
						className: 'btn btn-sm btn-success icon-refresh',
						action: function ( e, dt, node, config ) {
							that.refreshListH();
						}
					}]
				}
			}));
			DTConstant.enableRadio(this.$listH,this.tableH,function(selected,record){
				var title='激活/挂起';
				if(selected)
					title=record.suspensionStateCode?'激活':'挂起';
				$('.btn-state span',that.$div).text(title);
			});
		},
		refreshListH:function(){
			this.tableH.ajax.reload();
		},
		initListR:function(){
			var that=this;
			this.$listR=$('.procinstRList',this.$div);
			this.tableR =this.$listR.DataTable($.extend(true,{},DTConstant.DEFAULT,{
				ajax:{
				    url:this.getUrl('listR'),
				    contentType: "application/json",
				    type: "POST",
				    data: function ( d ) {
				      return $.toJSON(d);
				    }
				},
				rowId:'id',
				columns:[{data:'id',title:''},
				         {data: 'name' ,title:'名称'},
				         {data: 'processDefinitionName' ,title:'流程定义'},
				         {data: 'processDefinitionVersion' ,title:'版本'},
				         {data: 'activity',title:'当前节点'},
				         {data: 'suspended',title:'状态'}
				],
				columnDefs: [DTConstant.COLUMN.RADIO],
				buttons:{
					buttons: [{
						text: '激活/挂起',
						className: 'btn btn-sm btn-success icon-lightbulb btn-state',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							
							var title=(record.suspendedCode?'激活':'挂起')+'流程：'+(record.name||'');
							$.ajax(that.getUrl('updateStateProcinst'),{
								type:'POST',
								dataType:'json',
								data:{procinstId:record.id,state:record.suspendedCode}
							}).done(function(data){//成功
								if(data.result)
								{
									that.refreshListR();
									ACEConstant.operateSuccess(title);
									$('.btn-state span',that.$div).text('激活/挂起');
								}	
								else
									ACEConstant.operateFailed(title,'操作失败：'+data.note);							
							}).fail(function(data){//失败
								ACEConstant.operateFailed(title);
							});
						}
					},{
						text: '查看',
						className: 'btn btn-sm btn-success icon-eye-open',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							that.parent.toSubPage(ProcinstView,{
									procinstId:record.id,
									hasParentPage:true
								},'运行中的流程查看');
						}
					},{
						text: '删除',
						className: 'btn btn-sm btn-success icon-trash',
						action: function ( e, dt, node, config ) {
							var record=DTConstant.getSingleRecord(dt)
							if(!record)
								return;
							
							var title='删除运行中的流程实例：'+(record.name||'');
							$.ajax(that.getUrl('deleteProcinstR'),{
								type:'POST',
								dataType:'json',
								data:{procinstId:record.id}
							}).done(function(data){//成功
								if(data.result)
								{
									that.refreshListR();
									ACEConstant.operateSuccess(title);
								}	
								else
									ACEConstant.operateFailed(title,'操作失败：'+data.note);							
							}).fail(function(data){//失败
								ACEConstant.operateFailed(title);
							});
						}
					},{
						text: '刷新',
						className: 'btn btn-sm btn-success icon-refresh',
						action: function ( e, dt, node, config ) {
							that.refreshListR();
						}
					}]
				}
			}));
			DTConstant.enableRadio(this.$listR,this.tableR,function(selected,record){
				var title='激活/挂起';
				if(selected)
					title=record.suspendedCode?'激活':'挂起';
				$('.btn-state span',that.$div).text(title);
			});
		},
		refreshListR:function(){
			this.tableR.ajax.reload();
		},
		destroy:function(){
			this.$div.remove();
			this.$vmId&&delete avalon.vmodels[this.$vmId];
		}
	}
	
	Procinst.DEFAULT={
			
	};
	
	return Procinst;
});